package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SearchRecAdapter;
import flc.ast.databinding.ActivitySearchResultBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResSetBean;

public class SearchResultActivity extends BaseAc<ActivitySearchResultBinding> {

    public static String key;
    private SearchRecAdapter searchAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSearchResultBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSearch.setOnClickListener(this);

        mDataBinding.rvSearchList.setLayoutManager(new GridLayoutManager(mContext, 3));
        searchAdapter = new SearchRecAdapter();
        mDataBinding.rvSearchList.setAdapter(searchAdapter);
        searchAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_movie_name));
                    return;
                }
                getData();
                saveRecord();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        mDataBinding.etSearch.setText(key);
        getData();
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.MOVIE_SEARCH_DATA + key, StkResApi.createParamMap(0, 999), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        searchAdapter.setList(result);
                        mDataBinding.rvSearchList.setVisibility(View.VISIBLE);
                    } else {
                        mDataBinding.rvSearchList.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        listSave.add(key);
        List<String> listSp = AppSpUtils.getMovieSearchRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.remove(key);
            listSp.add(0, key);
            AppSpUtils.saveMovieSearchRecordList(listSp);
        } else {
            AppSpUtils.saveMovieSearchRecordList(listSave);
        }
    }
}